﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2019 Jiang Yin. All rights reserved.
// Homepage: http://gameframework.cn/
// Feedback: mailto:jiangyin@gameframework.cn
//------------------------------------------------------------

using System;
using GameFramework;
using UnityEngine;
using UnityGameFramework.Runtime;
using Object = UnityEngine.Object;

namespace StarForce
{
    public class BuiltinDataComponent : GameFrameworkComponent
    {
        [SerializeField] private TextAsset m_BuildInfoTextAsset = null;

        [SerializeField] private TextAsset m_DefaultDictionaryTextAsset = null;

        [SerializeField] private GameObject m_UpdateResourcePageTemplate = null;

        private BuildInfo m_BuildInfo = null;

        private RecourseUpdatePage m_UpdateResourcePage = null;

        public BuildInfo BuildInfo
        {
            get { return m_BuildInfo; }
        }

        public void InitBuildInfo()
        {
            if (m_BuildInfoTextAsset == null || string.IsNullOrEmpty(m_BuildInfoTextAsset.text))
            {
                Log.Info("Build info can not be found or empty.");
                return;
            }

            m_BuildInfo = Utility.Json.ToObject<BuildInfo>(m_BuildInfoTextAsset.text);
            if (m_BuildInfo == null)
            {
                Log.Warning("Parse build info failure.");
                return;
            }
        }

        public void InitDefaultDictionary()
        {
            if (m_DefaultDictionaryTextAsset == null || string.IsNullOrEmpty(m_DefaultDictionaryTextAsset.text))
            {
                Log.Info("Default dictionary can not be found or empty.");
                return;
            }

            return;
//            if (!GameEntry.Localization.ParseDictionary(m_DefaultDictionaryTextAsset.text))
//            {
//                Log.Warning("Parse default dictionary failure.");
//                return;
//            }
        }

        /// <summary>
        /// 加载资源更新
        /// </summary>
        public void LoadUpdateResourcePage()
        {
            if (m_UpdateResourcePage == null)
            {
                m_UpdateResourcePage = Instantiate(GameEntry.BuiltinData.m_UpdateResourcePageTemplate)
                    .GetComponent<RecourseUpdatePage>();
            }
        }

        /// <summary>
        /// 需要下载新包
        /// </summary>
        /// <param name="url"></param>
        public void NeedDownNewPackage(string url)
        {
            m_UpdateResourcePage.SetDownUrl(url);

            m_UpdateResourcePage.OpenNew();
        }

        /// <summary>
        /// 设置Tip
        /// </summary>
        /// <param name="txt"></param>
        public void SetProTxt(string txt)
        {
            if (m_UpdateResourcePage != null)
            {
                m_UpdateResourcePage.SetProgressTxt(txt);
            }
        }

        /// <summary>
        /// 修改更新进度
        /// </summary>
        /// <param name="progress"></param>
        /// <param name="description"></param>
        public void SetProgress(float progress, string description)
        {
            m_UpdateResourcePage.SetProgress(progress, description);
        }

        /// <summary>
        /// 销毁更新界面
        /// </summary>
        public void DestroyUpdateResourcePage()
        {
            if (m_UpdateResourcePage != null)
            {
                Object.Destroy(m_UpdateResourcePage.gameObject);
                m_UpdateResourcePage = null;
            }
        }

        private void Update()
        {
            if (Input.GetKey(KeyCode.Escape))
            {
                GameEntry.UI.OpenDialog(new DialogParams("是否确定退出？", "退出", Application.Quit, "再想想", null,
                    true));
            }
        }
    }
}